	

cd "/Users/sergi/Library/CloudStorage/Dropbox/Work/Coconut paper/Ethnic Minority British Electoral Survey/"

global path "/Users/sergi/Library/CloudStorage/Dropbox/Work/Coconut paper/ESS data/"

global pathfig "/Users/sergi/Library/CloudStorage/Dropbox/Aplicaciones/Overleaf/Migrant political integration/"

global results "/Users/sergi/Library/CloudStorage/Dropbox/Work/Coconut paper/Ethnic Minority British Electoral Survey/"




*Matching with ESS data

set scheme gg_tableau

use "$path/ESS1-8e01.dta", clear

encode cntry, gen(country)

numlabel country, add

tab country

drop if country==3 | country==5 | country==15 | country==18 | country==21 | country==27 | country==29 | country==32

drop if country==13 /*we drop the UK*/


drop if country==6 /*we drop Eastern Europe*/
drop if country==9
drop if country==16
drop if country==25
drop if country==30
drop if country==31



keep if brncntr==2

rename livecnta year /*rounds 5-8*/

gen age_migr_ess=year-yrbrn /*age when migrating*/

gen year_interview=.
replace year_interview=inwyr if essround<=2
replace year_interview=inwyys if essround>=3

gen length_stay_ess=year_interview-year


gen satdem_ess=stfdem/10

gen trust_politicians=trstprl+trstplt
sum trust_politicians
gen trust_politicians_ess=(trust_politicians-r(min))/(r(max)-r(min))


*factor trstprl trstlgl trstplc
*rotate, varimax
*predict poltrust_factor

*gen poltrust_factor_std=(poltrust_factor+2.18437) / (2.009282+2.18437)

*gen trstprl_std=trstprl/10

*gen trstlgl_std=trstlgl/10

*gen trstplc_std=trstplc/10

gen age_ess=agea

gen univdegree_ess=edulvla==5

gen male_ess=gndr==1

sum hinctnta,d
gen income_ess=(hinctnta-r(min))/(r(max)-r(min))
xtile income_cat_ess=hinctnta, nq(5)

rename uempla unemployed_ess

gen active_ess=mnactic==1 /*active: paid work vs others*/


gen vote_ess=vote==1

gen inter_trust= ppltrst/10

recode  dscrgrp 1=1 2=0, gen(discr_ess)

gen country_ess=country

keep country_ess satdem_ess trust_politicians_ess age_ess univdegree_ess male_ess income_ess income_cat_ess unemployed_ess vote_ess inter_trust discr_ess year age_migr_ess active_ess length_stay_ess

gen ess=1

save UK_ESS_2010.dta, replace
use embesapr11.dta, clear

append using UK_ESS_2010.dta



*numlabel _all, add force

gen commonwealth=0
replace commonwealth=. if  bq102_1 < 1
replace commonwealth=1 if  bq102_1 == 5
replace commonwealth=1 if  bq102_1 == 6
replace commonwealth=1 if  bq102_1 == 7
replace commonwealth=1 if  bq102_1 == 8
replace commonwealth=1 if  bq102_1 == 13
replace commonwealth=1 if  bq102_1 == 14
replace commonwealth=1 if  bq102_1 == 15
replace commonwealth=1 if  bq102_1 == 16
replace commonwealth=1 if  bq102_1 == 17
replace commonwealth=1 if  bq102_1 == 18
replace commonwealth=1 if  bq102_1 == 19
replace commonwealth=1 if  bq102_1 == 21
replace commonwealth=1 if  bq102_1 == 23
replace commonwealth=1 if  bq102_1 == 24
replace commonwealth=1 if  bq102_1 == 25
replace commonwealth=1 if  bq102_1 == 30
replace commonwealth=1 if  bq102_1 == 40
replace commonwealth=1 if  bq102_1 == 46
replace commonwealth=1 if  bq102_1 == 47
replace commonwealth=1 if  bq102_1 == 57
replace commonwealth=1 if  bq102_1 == 61
replace commonwealth=1 if  bq102_1 == 78
replace commonwealth=1 if  bq102_1 == 79
replace commonwealth=1 if  bq102_1 == 83
replace commonwealth=1 if  bq102_1 == 92
replace commonwealth=1 if  bq102_1 == 95
replace commonwealth=1 if  bq102_1 == 96
replace commonwealth=1 if  bq102_1 == 97
replace commonwealth=1 if  bq102_1 == 99
replace commonwealth=1 if  bq102_1 == 100
replace commonwealth=1 if  bq102_1 == 101
replace commonwealth=1 if  bq102_1 == 102
replace commonwealth=1 if  bq102_1 == 108
replace commonwealth=1 if  bq102_1 == 109
replace commonwealth=1 if  bq102_1 == 113
replace commonwealth=1 if  bq102_1 == 124
replace commonwealth=1 if  bq102_1 == 125



recode bq61 1=4 2=3 3=2 4=1 else=., gen(satdem)
gen satdem_embes_std=(satdem-1)/(4-1)

recode bq16_2 -2/-1=., gen(trustparl)

recode bq16_3 -2/-1=., gen(trust_politicians)

gen poltrust_embes = trustparl+trust_politicians
sum poltrust_embes
gen poltrust_embes_std = (poltrust_embes-r(min)) / (r(max)-r(min))



*recode bq16_4 -2/-1=., gen(trust_police)
*gen trustpolice_embes_std=(trust_police-1)/(11-1)


capture drop inter_trust
recode eq15 1=1 2/3=0 else=., gen(inter_trust)

gen univdegree=0
replace univdegree=1 if  bq95_3==1
replace univdegree=1 if  bq95_3==2
replace univdegree=1 if  eq64_2==2
replace univdegree=1 if  eq64_2==3

gen income_embes=bq96
replace income_embes=. if bq96<1
sum income_embes, d
gen income_embes_std=(income_embes-r(min))/(r(max)-r(min))
xtile income_cat_embes=income_embes, nq(5)

 
recode bq88 1=1 2=0, gen(male)
gen age=bq89
gen age_sq=age^2


gen unemployed=.
replace unemployed=1 if eq65_1==3
replace unemployed=1 if eq65_1==4
replace unemployed=0 if eq65_1==1

recode eq65_1 1=1 2/8=0 else=., gen(active_embes)



recode eq37 1=1 2=0 else=., gen(discr_embes)

recode bq12_1 1=1 2=0 else=., gen(voted)


capture drop year_temp
capture drop year_arrived
gen year_temp = 2010 - bq102_2r if bq102_2r < 1947
gen year_arrived = .
replace year_arrived = bq102_2r if bq102_2r > 1946
replace year_arrived = year_temp if year_temp !=.

replace year_arrived=year if ess==1

drop if year_arrived<1948
drop if year_arrived>2010



gen year_born_embes=2010-age
gen age_migr_embes=year_arrived-year_born_embes
gen length_stay_embes=2010-year_arrived


recode ess .=0

gen satdem_good=.
replace satdem_good=satdem_ess if ess==1
replace satdem_good=satdem_embes_std if ess==0

gen poltrust_good=.
replace poltrust_good=trust_politicians_ess if ess==1
replace poltrust_good=poltrust_embes_std if ess==0

*gen poltrustparl_good=.
*replace poltrustparl_good=trstprl_std if ess==1
*replace poltrustparl_good=trustparl_embes_std if ess==0


*gen poltrustpolice_good=.
*replace poltrustpolice_good=trstplc_std if ess==1
*replace poltrustpolice_good=trustpolice_embes_std if ess==0



*alpha poltrustparl_good poltrustpolice_good
*gen trustgood_additive=poltrustparl_good+poltrustpolice_good


*factor poltrustparl_good poltrustpolice_good
*rotate, varimax
*predict trustgood_factor

gen educ_good=.
replace educ_good=univdegree_ess if ess==1
replace educ_good=univdegree if ess==0
label var educ_good"university degree"

gen male_good=.
replace male_good=male_ess if ess==1
replace male_good=male if ess==0

gen age_good=.
replace age_good=age_ess if ess==1
replace age_good=age if ess==0

gen age_goodsq=age_good^2

gen age_good3=age_good^3
gen age_good4=age_good^4


gen unemployed_good=.
replace unemployed_good=unemployed_ess if ess==1
replace unemployed_good=unemployed if ess==0

gen income_good=.
replace income_good=income_ess if ess==1
replace income_good=income_embes_std if ess==0

gen income_cat_good=.
replace income_cat_good=income_cat_ess if ess==1
replace income_cat_good=income_cat_embes if ess==0

gen particip_good=.
replace particip_good=vote_ess if ess==1
replace particip_good=voted if ess==0


gen discr_good=.
replace discr_good=discr_ess if ess==1
replace discr_good=discr_embes if ess==0


gen agemigr_good=.
replace agemigr_good=age_migr_ess if ess==1
replace agemigr_good=age_migr_embes if ess==0

gen active_good=.
replace active_good=active_ess if ess==1
replace active_good=active_embes if ess==0

gen length_stay_good=.
replace length_stay_good=length_stay_ess if ess==1
replace length_stay_good=length_stay_embes if ess==0

*Region


label dir
label list bq102_1

gen region=0
replace region=. if  bq102_1 < 1
replace region=. if bq102_1 > 131

*Leaving as they are (0): England(1), Scotland(2), Wales(3), Ireland(4), 


*Asia
replace region=1 if  bq102_1 ==5
replace region=1 if  bq102_1 ==6
replace region=1 if  bq102_1 ==7
replace region=1 if  bq102_1 ==27
replace region=1 if  bq102_1 ==39
replace region=1 if  bq102_1 ==50
replace region=1 if  bq102_1 ==61
replace region=1 if  bq102_1 ==66
replace region=1 if  bq102_1 ==68
replace region=1 if  bq102_1 ==69
replace region=1 if  bq102_1 ==70
replace region=1 if  bq102_1 ==74
replace region=1 if  bq102_1 ==78
replace region=1 if  bq102_1 ==87
replace region=1 if  bq102_1 ==93
replace region=1 if  bq102_1 ==97
replace region=1 if  bq102_1 ==99
replace region=1 if  bq102_1 ==103
replace region=1 if  bq102_1 ==104
replace region=1 if  bq102_1 ==118
replace region=1 if  bq102_1 ==119
replace region=1 if  bq102_1 ==123
replace region=1 if  bq102_1 ==125
replace region=1 if  bq102_1 ==126

*Caribbean
replace region=2 if  bq102_1 ==8
replace region=2 if  bq102_1 ==14
replace region=2 if  bq102_1 ==15
replace region=2 if  bq102_1 ==21
replace region=2 if  bq102_1 ==24
replace region=2 if  bq102_1 ==30
replace region=2 if  bq102_1 ==31
replace region=2 if  bq102_1 ==32
replace region=2 if  bq102_1 ==36
replace region=2 if  bq102_1 ==38
replace region=2 if  bq102_1 ==43
replace region=2 if  bq102_1 ==53
replace region=2 if  bq102_1 ==57
replace region=2 if  bq102_1 ==58
replace region=2 if  bq102_1 ==81
replace region=2 if  bq102_1 ==100
replace region=2 if  bq102_1 ==101
replace region=2 if  bq102_1 ==102
replace region=2 if  bq102_1 ==112
replace region=2 if  bq102_1 ==113
replace region=2 if  bq102_1 ==115
replace region=2 if  bq102_1 ==116
replace region=2 if  bq102_1 ==124

*Africa
replace region=3 if  bq102_1 ==9
replace region=3 if  bq102_1 ==10
replace region=3 if  bq102_1 ==12
replace region=3 if  bq102_1 ==13
replace region=3 if  bq102_1 ==16
replace region=3 if  bq102_1 ==17
replace region=3 if  bq102_1 ==18
replace region=3 if  bq102_1 ==19
replace region=3 if  bq102_1 ==20
replace region=3 if  bq102_1 ==22
replace region=3 if  bq102_1 ==23
replace region=3 if  bq102_1 ==25
replace region=3 if  bq102_1 ==26
replace region=3 if  bq102_1 ==28
replace region=3 if  bq102_1 ==29
replace region=3 if  bq102_1 ==40
replace region=3 if  bq102_1 ==44
replace region=3 if  bq102_1 ==45
replace region=3 if  bq102_1 ==46
replace region=3 if  bq102_1 ==48
replace region=3 if  bq102_1 ==59
replace region=3 if  bq102_1 ==60
replace region=3 if  bq102_1 ==64
replace region=3 if  bq102_1 ==72
replace region=3 if  bq102_1 ==75
replace region=3 if  bq102_1 ==76
replace region=3 if  bq102_1 ==79
replace region=3 if  bq102_1 ==82
replace region=3 if  bq102_1 ==83
replace region=3 if  bq102_1 ==84
replace region=3 if  bq102_1 ==92
replace region=3 if  bq102_1 ==95
replace region=3 if  bq102_1 ==96
replace region=3 if  bq102_1 ==108
replace region=3 if  bq102_1 ==109
replace region=3 if  bq102_1 ==110
replace region=3 if  bq102_1 ==114
replace region=3 if  bq102_1 ==128
replace region=3 if  bq102_1 ==129

*Withe Commonwealth
replace region=4 if  bq102_1 ==34
replace region=4 if  bq102_1 ==47

*Other
replace region=5 if  bq102_1 ==11
replace region=5 if  bq102_1 ==31
replace region=5 if  bq102_1 ==35
replace region=5 if  bq102_1 ==37
replace region=5 if  bq102_1 ==41
replace region=5 if  bq102_1 ==42
replace region=5 if  bq102_1 ==49
replace region=5 if  bq102_1 ==51
replace region=5 if  bq102_1 ==52
replace region=5 if  bq102_1 ==54
replace region=5 if  bq102_1 ==55
replace region=5 if  bq102_1 ==56
replace region=5 if  bq102_1 ==62
replace region=5 if  bq102_1 ==63
replace region=5 if  bq102_1 ==65
replace region=5 if  bq102_1 ==67
replace region=5 if  bq102_1 ==71
replace region=5 if  bq102_1 ==73
replace region=5 if  bq102_1 ==77
replace region=5 if  bq102_1 ==80
replace region=5 if  bq102_1 ==85
replace region=5 if  bq102_1 ==86
replace region=5 if  bq102_1 ==88
replace region=5 if  bq102_1 ==89
replace region=5 if  bq102_1 ==90
replace region=5 if  bq102_1 ==91
replace region=5 if  bq102_1 ==98
replace region=5 if  bq102_1 ==105
replace region=5 if  bq102_1 ==106
replace region=5 if  bq102_1 ==107
replace region=5 if  bq102_1 ==111
replace region=5 if  bq102_1 ==117
replace region=5 if  bq102_1 ==120
replace region=5 if  bq102_1 ==121
replace region=5 if  bq102_1 ==122
replace region=5 if  bq102_1 ==127
replace region=5 if  bq102_1 ==130
replace region=5 if  bq102_1 ==131


gen whitecommon=region==4

sort year_arrived

capture drop  treated 

gen treated=.
replace treated=1 if commonwealth==1
replace treated=0 if ess==1

foreach i of num  1962 1973 1983{
capture drop  intervention_`i' time_interv_`i' treated_time_`i' treated_interv_`i' treated_interv_time_`i'
gen time_elapsed_`i'=year_arrived-`i'
gen intervention_`i'=.
replace intervention_`i'=0 if year_arrived<`i'
replace intervention_`i'=1 if year_arrived>=`i'
gen time_interv_`i'=time_elapsed_`i'*intervention_`i'
gen treated_time_`i'=treated*time_elapsed_`i'
gen treated_interv_`i'=treated*intervention_`i'
gen treated_interv_time_`i'=treated*intervention_`i'*time_elapsed_`i'
}

gen income_log=log(income_good)


tab country_ess

gen country_good=.
replace country_good=country_ess if ess==1
replace country_good=0 if ess==0

tab bq102_1 if year_arrived >=1962 & year_arrived <=1972 /*to see how many people affected by 1968 commonwealth act, restricting access to East Africans*/


*Figure 2 - description Commonwealth migrants across policy regimes


egen number = count(year_arrived), by(year_arrived)

tw line number year_arrived if agemigr_good>=18 & whitecommon !=1 & year_arrived<1974 & ess==0, lcolor(black) xlabel(1952(1)1973, angle(90)) ///
xtitle("Year arrived", size(6)) ytitle("Density", size(6)) ///
xline(1962, lcolor(red)) xline(1973, lcolor(red)) ylabel(0(50)250) text(220 1957 "Free movement" 220 1968 "Watermelon regime") ///
text(25 1965 "--> Commonwealth Immigrants Act" 48 1970 "Immigration Act comes to force <--", size(2)) /*numbers increase but if anything just before policy change. Beat the ban, consistent with Massey et al*/

graph export "$pathfig/mechanism_numbers.pdf", as(pdf) replace


*Table 1 - Difference in differences models (migrants as units of analysis)


xtset year_arrived

xtreg satdem_good treated intervention_1962  treated_interv_1962 age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973, fe vce(cluster year_arrived)

est store diff1_migrants

xtreg poltrust_good treated intervention_1962  treated_interv_1962 age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973, fe vce(cluster year_arrived) 

est store diff2_migrants

*Appendix A - Table A.2: Descriptive stats

sum satdem_good poltrust_good treated intervention_1962   age_good educ_good male_good 


*Appendix C - Table A.5: Unit specific trends (models with migrants as control group)

gen trend=year_arrived-1948


xtreg satdem_good treated intervention_1962  treated_interv_1962 treated##c.trend age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973, fe vce(cluster year_arrived) 

est store diff5_migrants

xtreg poltrust_good treated intervention_1962  treated_interv_1962 treated##c.trend age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973, fe vce(cluster year_arrived) 

est store diff6_migrants

*Appendix D - Table A.6 with country and year dummies

reg satdem_good treated intervention_1962  treated_interv_1962 age_good educ_good male_good ib0.country_good i.year_arrived ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973, vce(cluster year_arrived)

reg poltrust_good treated intervention_1962  treated_interv_1962 age_good educ_good male_good ib0.country_good i.year_arrived ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973, vce(cluster year_arrived) 


*Appendix E - Table A.7 - without fixed-effects



xtreg satdem_good treated intervention_1962  treated_interv_1962 age_good educ_good male_good ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973, re vce(cluster year_arrived)

xtreg poltrust_good treated intervention_1962  treated_interv_1962 age_good educ_good male_good  ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973, re vce(cluster year_arrived) 



*Appendix F- Table A.8: Weighting by propensity to get treated

capture drop propensity 
logit treated age_good educ_good male_good agemigr_good 
predict propensity, pr


reg satdem_good treated intervention_1962  treated_interv_1962 age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973 [aweight=propensity], vce(cluster year_arrived)

est store weighted1_migrants

reg poltrust_good treated intervention_1962  treated_interv_1962 age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973 [aweight=propensity], vce(cluster year_arrived) 

est store weighted2_migrants



*Appendix K - Table A.13: Excluding migrants from Uganda, and Kenya

numlabel bq102_1, add
tab bq102_1

gen east_africa=0
replace east_africa=1 if bq102_1==25
replace east_africa=1 if bq102_1==16


xtreg satdem_good treated intervention_1962  treated_interv_1962 age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973 & east_africa==0, fe vce(cluster year_arrived)

est store diff1_migrants_eastafrica

xtreg poltrust_good treated intervention_1962  treated_interv_1962 age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973 & east_africa==0, fe vce(cluster year_arrived) 

est store diff2_migrants_eastafrica





*Figure 2 - Mechanisms


gen cohort=.
replace cohort=1 if year_arrived>=1948 & year_arrived <=1961
replace cohort=2 if year_arrived>=1962 & year_arrived <=1972
replace cohort=3 if year_arrived>=1973 & year_arrived <=1983



gen coconut_free = .
replace coconut_free=1 if cohort==2
replace coconut_free=0 if cohort==1

gen hard_coconut = .
replace hard_coconut=1 if cohort==3
replace hard_coconut=0 if cohort==2

recode bq106_a 1=1 3=2 4=3 5=4 6/7=5, gen(religion)


logit coconut_free   agemigr_good educ_good i.region ib3.religion if treated==1

coefplot, xline(0, lcolor(black)) drop(_cons) coeflabel(agemigr_good = "Age migration" educ_good = "University degree" 1.region = "Asia" 2.region = "Caribbean" 3.region = "Africa" 1.religion = "Christian" 2.religion = "Hindu" 3.religion = "Muslim" 4.religion = "Sikh" 5.religion = "Buddhist & other") baselevels mcolor(black) ciopts(lcolor(black))  name(mec1_1962, replace)

graph export "$pathfig/mechanism1_coconut.pdf", as(pdf) replace




logit coconut_free eq60_1b eq60_1d eq60_1e eq60_1f  if treated==1 

coefplot, xline(0, lcolor(black)) drop(_cons) coeflabel(eq60_1b = "Joining spouse" eq60_1d = "Better life" eq60_1e = "Freedom" eq60_1f = "Earn money") ///
mcolor(black) ciopts(lcolor(black)) name(mec2_1962, replace)

graph export "$pathfig/mechanism2_coconut.pdf", as(pdf) replace


numlabel bq102_1, add
tab bq102_1

gen COWcode=.
replace COWcode=750 if bq102_1==5
replace COWcode=770 if bq102_1==6
replace COWcode=771 if bq102_1==7
replace COWcode=53 if bq102_1==8
replace COWcode=484 if bq102_1==9
replace COWcode=531 if bq102_1==10
replace COWcode=220 if bq102_1==11
replace COWcode=420 if bq102_1==12
replace COWcode=452 if bq102_1==13
replace COWcode=55 if bq102_1==14
replace COWcode=51 if bq102_1==15
replace COWcode=501 if bq102_1==16
replace COWcode=590 if bq102_1==17
replace COWcode=475 if bq102_1==18
replace COWcode=560 if bq102_1==19
replace COWcode=520 if bq102_1==20
replace COWcode=56 if bq102_1==21
replace COWcode=625 if bq102_1==22
replace COWcode=510 if bq102_1==23
replace COWcode=52 if bq102_1==24
replace COWcode=500 if bq102_1==25
replace COWcode=552 if bq102_1==26
replace COWcode=700 if bq102_1==27
replace COWcode=615 if bq102_1==28
replace COWcode=540 if bq102_1==29
replace COWcode=58 if bq102_1==30
replace COWcode=571 if bq102_1==40
replace COWcode=516 if bq102_1==45
replace COWcode=471 if bq102_1==46
replace COWcode=20 if bq102_1==47
replace COWcode=402 if bq102_1==48
replace COWcode=40 if bq102_1==53
replace COWcode=54 if bq102_1==57
replace COWcode=42 if bq102_1==58
replace COWcode=651 if bq102_1==59
replace COWcode=530 if bq102_1==60
replace COWcode=950 if bq102_1==61
replace COWcode=255 if bq102_1==63
replace COWcode=210 if bq102_1==65
replace COWcode=437 if bq102_1==72
replace COWcode=690 if bq102_1==74
replace COWcode=450 if bq102_1==75
replace COWcode=620 if bq102_1==76
replace COWcode=820 if bq102_1==78
replace COWcode=553 if bq102_1==79
replace COWcode=600 if bq102_1==82
replace COWcode=541 if bq102_1==83
replace COWcode=235 if bq102_1==86
replace COWcode=840 if bq102_1==87
replace COWcode=517 if bq102_1==92
replace COWcode=670 if bq102_1==93
replace COWcode=591 if bq102_1==95
replace COWcode=451 if bq102_1==96
replace COWcode=830 if bq102_1==97
replace COWcode=230 if bq102_1==98
replace COWcode=780 if bq102_1==99
replace COWcode=60 if bq102_1==100
replace COWcode=57 if bq102_1==101
replace COWcode=52 if bq102_1==102
replace COWcode=2 if bq102_1==106
replace COWcode=551 if bq102_1==108
replace COWcode=511 if bq102_1==109
replace COWcode=403 if bq102_1==110
replace COWcode=380 if bq102_1==111
replace COWcode=110 if bq102_1==124
replace COWcode=385 if bq102_1==127
replace COWcode=516 if bq102_1==128

capture drop year
gen year=year_arrived


capture drop _merge
merge m:m COWcode year using "$path/V-Dem.dta",  keepusing(e_gdppc v2dlencmps v2x_libdem  )



logit coconut_free e_gdppc v2dlencmps  v2x_libdem  if treated==1 

coefplot, xline(0, lcolor(black)) drop(_cons) coeflabel(e_gdppc = "GDP per capita" v2dlencmps = "Particularistic vs. public goods"  v2x_libdem = "Liberal democracy") ///
mcolor(black) ciopts(lcolor(black)) name(mec3_1962, replace)

graph export "$pathfig/mechanism3_coconut.pdf", as(pdf) replace



*Appendix L - Social discrimination as a mechanism diff-in-diff

*Table A.14

xtreg discr_good treated intervention_1962  treated_interv_1962 age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973, fe vce(cluster year_arrived)

est store diff1_migrants_discr

*Table A.15

reg discr_good time_elapsed_1962 intervention_1962 time_interv_1962 treated treated_time_1962 treated_interv_1962 treated_interv_time_1962 age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1, vce(cluster year_arrived)
est store its_discr1

*Table A.16

recode eq38 1=3 2=2 3=1 else=., gen(discr_freq)

reg discr_embes ib1.cohort age_good educ_good male_good if treated==1 & agemigr_good>=18 & whitecommon !=1, vce(cluster year_arrived)


reg discr_freq ib1.cohort age_good educ_good male_good if treated==1 & agemigr_good>=18 & whitecommon !=1, vce(cluster year_arrived)



	
*Table 2 - INTERRUPTED TIMES SERIES ANALYSES (migrant vs migrant comparisons) and corresponding figures appendix G


*Satisfaction democracy

sort year_arrived


reg satdem_good time_elapsed_1962 intervention_1962 time_interv_1962 treated treated_time_1962 treated_interv_1962 treated_interv_time_1962 age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1, vce(cluster year_arrived)
est store its_inc1



est restore its_inc1

capture drop mv
gen mv=_n-1 if _n<11

matrix define its2_beta=e(b)
scalar define int_1962=its2_beta[1,6]
scalar define slope_1962=its2_beta[1,7]
matrix define its2_v=e(V)
scalar define var_int=its2_v[6,6]
scalar define var_slope=its2_v[7,7]
scalar define covar_inter_slope=its2_v[6,7]

capture drop marginal_unemp_1962 error_unemp_1962 upper_unemp_1962 lower_unemp_1962

gen marginal_unemp_1962=int_1962+(slope_1962*mv)
gen error_unemp_1962=sqrt(var_int+var_slope*(mv^2)+2*covar_inter_slope*mv)
gen upper_unemp_1962=marginal_unemp_1962+(1.96*error_unemp_1962)
gen lower_unemp_1962=marginal_unemp_1962-(1.96*error_unemp_1962)


graph twoway scatter marginal_unemp_1962 mv || rspike upper_unemp_1962 lower_unemp_1962 mv, lc(black) xlabel(0(1)9) ///  
 yline(0) ylabel(-0.5 0 0.5) yline(0, lcolor(black)) xtitle(Years after policy change, size(6)) ytitle(1962 policy effect, size(6)) legend(off)

graph export "$pathfig/its_satdem62_migrants.pdf", as(pdf) replace

*Political Trust

reg poltrust_good time_elapsed_1962 intervention_1962 time_interv_1962 treated treated_time_1962 treated_interv_1962 treated_interv_time_1962 age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1, vce(cluster year_arrived)
est store its_inc2




est restore its_inc2

capture drop mv
gen mv=_n-1 if _n<11

matrix define its2_beta=e(b)
scalar define int_1962=its2_beta[1,6]
scalar define slope_1962=its2_beta[1,7]
matrix define its2_v=e(V)
scalar define var_int=its2_v[6,6]
scalar define var_slope=its2_v[7,7]
scalar define covar_inter_slope=its2_v[6,7]

capture drop marginal_unemp_1962 error_unemp_1962 upper_unemp_1962 lower_unemp_1962

gen marginal_unemp_1962=int_1962+(slope_1962*mv)
gen error_unemp_1962=sqrt(var_int+var_slope*(mv^2)+2*covar_inter_slope*mv)
gen upper_unemp_1962=marginal_unemp_1962+(1.96*error_unemp_1962)
gen lower_unemp_1962=marginal_unemp_1962-(1.96*error_unemp_1962)


graph twoway scatter marginal_unemp_1962 mv || rspike upper_unemp_1962 lower_unemp_1962 mv, lc(black) xlabel(0(1)9) ///  
 yline(0) ylabel(-0.5 0 0.5)  yline(0, lcolor(black)) xtitle(Years after policy change, size(6)) ytitle(1962 policy effect, size(6)) legend(off)

graph export "$pathfig/its_poltrust62_migrants.pdf", as(pdf) replace






*Appendix B - Parallel tests (models with migrants as control group)



foreach i of num  1950(1)1965 {
capture drop time_elapsed_`i' intervention_`i' time_interv_`i' treated_time_`i' treated_interv_`i' treated_interv_time_`i'
gen time_elapsed_`i'=year_arrived-`i'
gen intervention_`i'=.
replace intervention_`i'=0 if year_arrived<`i'
replace intervention_`i'=1 if year_arrived>=`i'
gen time_interv_`i'=time_elapsed_`i'*intervention_`i'
gen treated_time_`i'=treated*time_elapsed_`i'
gen treated_interv_`i'=treated*intervention_`i'
gen treated_interv_time_`i'=treated*intervention_`i'*time_elapsed_`i'
}


capture drop inter_binned
gen inter_binned =.
replace inter_binned=1 if intervention_1950==1 & intervention_1951==1
replace inter_binned=2 if intervention_1952==1 & intervention_1953==1
replace inter_binned=3 if intervention_1954==1 & intervention_1955==1
replace inter_binned=4 if intervention_1956==1 & intervention_1957==1
replace inter_binned=5 if intervention_1958==1 & intervention_1959==1
replace inter_binned=6 if intervention_1960==1 & intervention_1961==1
replace inter_binned=7 if intervention_1962==1 & intervention_1963==1
replace inter_binned=8 if intervention_1964==1 & intervention_1965==1

xtreg satdem_good treated##ib6.inter_binned age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973, fe vce(cluster year_arrived)


capture matrix drop coef
matrix coef = J(8, 3, .)

foreach i of num  1(1)8 {
matrix coef[`i',1]=_b[1.treated#`i'.inter_binned]
matrix coef[`i',2]=_b[1.treated#`i'.inter_binned] - _se[1.treated#`i'.inter_binned]*1.96
matrix coef[`i',3]=_b[1.treated#`i'.inter_binned] + _se[1.treated#`i'.inter_binned]*1.96
}

capture  drop coef1 coef2 coef3
capture drop beta low_ci high_ci
svmat coef
rename coef1 beta
rename coef2 low_ci
rename coef3 high_ci

capture drop axis_gr
capture label drop axis_gr
gen axis_gr=_n
replace axis_gr=. if axis_gr>8
label def axis_gr 1"1950-1951" 2"1952-1953" 3"1954-1955" 4"1956-1957" 5"1958-1959" 6"1960-1961" 7"1962-1963" 8"1964-1965"
label val axis_gr axis_gr


graph twoway (scatter  beta axis_gr if axis_gr<8, msymbol(circle) msize(small) mcolor(black) xline(6.5, lcolor(red) lp(solid)) yline(0, lcolor(black))  ///
 graphregion(color(white)) title("", size(small)) ///
 xtitle("Year of arrival", size(6)) xlabel(#7, valuelabel labsize(small)) ///
 ylabel(-1 0 1, labsize(small)) ytitle("", size(small)) legend(off)) ///
 (rspike low_ci high_ci axis_gr if axis_gr<8, lcolor(black) vertical)

 graph export "$pathfig/parallel_satdem62_migrants.pdf", as(pdf) replace


xtreg poltrust_good treated##ib6.inter_binned age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973, fe vce(cluster year_arrived)


capture matrix drop coef
matrix coef = J(8, 3, .)

foreach i of num  1(1)8 {
matrix coef[`i',1]=_b[1.treated#`i'.inter_binned]
matrix coef[`i',2]=_b[1.treated#`i'.inter_binned] - _se[1.treated#`i'.inter_binned]*1.96
matrix coef[`i',3]=_b[1.treated#`i'.inter_binned] + _se[1.treated#`i'.inter_binned]*1.96
}

capture  drop coef1 coef2 coef3
capture drop beta low_ci high_ci
svmat coef
rename coef1 beta
rename coef2 low_ci
rename coef3 high_ci

capture drop axis_gr
capture label drop axis_gr
gen axis_gr=_n
replace axis_gr=. if axis_gr>8
label def axis_gr 1"1950-1951" 2"1952-1953" 3"1954-1955" 4"1956-1957" 5"1958-1959" 6"1960-1961" 7"1962-1963" 8"1964-1965"
label val axis_gr axis_gr


graph twoway (scatter  beta axis_gr if axis_gr<8, msymbol(circle) msize(small) mcolor(black) xline(6.5, lcolor(red) lp(solid)) yline(0, lcolor(black))  ///
 graphregion(color(white)) title("", size(small)) ///
 xtitle("Year of arrival", size(6)) xlabel(#7, valuelabel labsize(small)) ///
 ylabel(-1 0 1, labsize(small)) ytitle("", size(small)) legend(off)) ///
 (rspike low_ci high_ci axis_gr if axis_gr<8, lcolor(black) vertical)

 graph export "$pathfig/parallel_poltrust62_migrants.pdf", as(pdf) replace



 




*Appendix M - Table A.17: Without control group

reg satdem_good coconut_free age_good educ_good male_good  ///
if treated==1, vce(cluster year_arrived)


reg poltrust_good coconut_free age_good educ_good male_good ///
if treated==1, vce(cluster year_arrived)


*Appendix O - Table A.19: Overall outcome

factor satdem_good poltrust_good 
rotate, varimax
predict overall_factor


xtreg overall_factor treated intervention_1962  treated_interv_1962 age_good educ_good male_good ib0.country_good ///
if agemigr_good>=18 & whitecommon !=1 & year_arrived<1973, fe vce(cluster year_arrived)



 *Appendix H: Time series tests: Prais-Winsten regression with Feasible Generalised Least Squares to deal with potential first-order serial correlation


sort year_arrived

preserve /*run this in one go until last PRAIS model*/
collapse (mean) satdem_good poltrust_good time_elapsed_1962 intervention_1962 time_interv_1962  treated_time_1962 treated_interv_1962 treated_interv_time_1962 time_elapsed_1973 intervention_1973 time_interv_1973  treated_time_1973 treated_interv_1973 treated_interv_time_1973 age_good educ_good male_good  if agemigr_good>=18 & whitecommon !=1, by(year_arrived treated)

tsset treated year_arrived 


prais satdem_good time_elapsed_1962 intervention_1962 time_interv_1962 treated treated_time_1962 treated_interv_1962 treated_interv_time_1962 age_good educ_good male_good, vce(robust)
est store prais_satdem_62
outreg2  using "${results}prais_1962_models.xls",  bdec(3) sdec(3) append

prais poltrust_good time_elapsed_1962 intervention_1962 time_interv_1962 treated treated_time_1962 treated_interv_1962 treated_interv_time_1962 age_good educ_good male_good, vce(robust)
est store prais_poltrust_62
outreg2  using "${results}prais_1962_models.xls",  bdec(3) sdec(3) append


